<?php

namespace app\admin\controller\merchant;

class Order extends Base
{
    protected $model = null;

    protected $searchFields = 'uid|spreadid|said|out_trade_no|spbill_create_ip|sub_mch_id';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = loader('admin')->loadModel('jy\\Order');
        $this->model->alias('O');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->join('order_logistics OL', 'O.id = OL.oid', 'left')
                ->where('delete', 0)
                ->where($where)
                ->order($sort, $order)
                ->field('O.*,OL.tracking')
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        $profitSharingOptions = loadEnum('ProfitSharing')->get_options();
        $statusOptions = loadEnum('OrderStatus')->get_options();
        $productClassifyOptions = loadEnum('ProductClassify')->get_options();

        $this->assign('profitSharingOptions', $profitSharingOptions);
        $this->assign('statusOptions', $statusOptions);
        $this->assign('productClassifyOptions', $productClassifyOptions);
        return $this->view->fetch();
    }

    /**
     * 查看
     */
    public function one()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->join('order_logistics OL', 'O.id = OL.oid', 'left')
                ->where('delete', 0)
                ->where($where)
                ->order($sort, $order)
                ->field('O.*,OL.tracking')
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }

    /*
     *  设置订单消息跳转路径
     */
    public function set_msg_jump_path() {
        $model = loadModel('jy\\Config');
        $row = $model->where(['type' => 'order'])->column('key,value');
        if ($this->request->isAjax()) {
            $post = $this->request->post('row/a');
            try {
                $model->startTrans();
                if(isset($row['path'])) {
                    $model->where(['type' => 'order', 'key' => 'path'])->update(['value' => $post['path']]);
                } else {
                    $model->save(['type' => 'order', 'key' => 'path', 'value' => $post['path']]);
                }
                loader('admin')->loadProcess('Order')->set_msg_jump_path_ipl($post['path']);
                $model->commit();
            } catch(\Exception $e) {
                $model->rollback();
                return $this->error($e->getMessage());
            }
            return $this->success('保存成功');
        }
        $this->assign('row', $row);
        return $this->view->fetch();
    }

    //分账
    public function profit_sharing() {
        $id = $this->request->param('id');
        $validate = loader('admin')->loadValidate('ProfitSharing');
        if(!$validate->scene('profit_sharing')->check(['id' => $id])) {
            $this->error($validate->getError());
        }

        $order = loadOnly('Base')->get('jy_order__' . $id, function($id) {
            return loader('admin')->loadModel('jy\\Order')->find($id);
        }, [$id]);
        $tool = 'profit_sharing\\StstemMultiple';
        if($order->getData('spreadid')) {
            $tool = 'profit_sharing\\GroupBuyingMultiple';
        }
        /*$user = loadModel('tyyh\\User')->find($order['uid']);
        if($user['spreadid']) {
            $tool = 'profit_sharing\\ShareholderMultiple';
        }*/
        try {
            loadTool($tool)->share($order);
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success('分账成功');


    }

    //股东分账
    private function shareholder($order, $spreadid) {
        
    }
}
